package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Author：毕龙
 * Date: 2023/7/27 9:30
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //乱码处理
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");

        System.out.println("请求hello 成功");
        //获取请求的参数
        String name = req.getParameter("name");

        //获取数据
        Object food = req.getAttribute("food");


        //使用响应对象
        PrintWriter pw = resp.getWriter();
        //将字符数据以打印流响应给浏览器
        pw.write("hello nihaoshijie!");
        //请求的参数响应界面
        pw.write(name);
        pw.write(food.toString());
    }
}
